/** 
 * Description:用户查询
 * Date:2011-11-15
 * Author:Along	
*/
package com.community.shopping.web;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.community.shopping.domain.User;
import com.community.shopping.service.logic.OrderManager;
import com.community.shopping.service.logic.Page;
import com.community.shopping.service.logic.UserManager;



@Controller 
public class UserControl {

	@Autowired  
    private UserManager userManager;
	
	@Autowired  
    private OrderManager orderManager;
	
	/**
	 * 查询返利的下级用户
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("/query/backuser")
	public void queryUserScore(HttpServletRequest request,HttpServletResponse response)throws Exception
	{
		String userId=request.getParameter("userId");
		User currentuser=userManager.getUserById(userId);
		Page page=new Page(request,8);	
		List<User> rebackusers= userManager.getUserByHql(" regfrom="+userId ,page);
		List<User> allbackusers=userManager.getUserByHql(" regfrom="+userId);
		String ids="0";
		for(User user:allbackusers)
		{
			ids=ids+","+user.getId();		
		}
		StringBuilder sb=new StringBuilder("{\"users\":[");
		float allprice=0;
		for(User user:rebackusers)
		{
			String filter=" orderUser.id ="+user.getId()+" and status=20 ";			
			String []result=orderManager.queryRebackSum(filter).split("_");
			String count=result[0].equals("null")?"0":result[0];;
			String totalprice=result[1].equals("null")?"0":result[1];
			allprice=allprice+Float.parseFloat(totalprice);
			if(sb.length()>20)
			{
				sb.append(",");	
			}
			String username=user.getUserName();
			username=username.substring(0, 3)+"****"+username.substring(7);
			//String address="["+user.getAddress().substring(0,4)+"]";
			String createDate=user.getCreateDate().substring(0,10);
			sb.append("{");
			sb.append("\"userid\":\""+user.getId()+"\",");
			sb.append("\"username\":\""+username+"\",");
			sb.append("\"count\":\""+count+"\",");
			sb.append("\"totalprice\":\""+totalprice+"\",");
			sb.append("\"createdate\":\""+createDate+"\"");
			sb.append("}");	
			
		}
		String allfilter=" orderUser.id in ("+ids+") and status=20 ";			
		String []result=orderManager.queryRebackSum(allfilter).split("_");
		String totalprice=result[1].equals("null")?"0":result[1];
		
		sb.append("],\"pageno\":\""+page.getCurrentPage()+"\",\"totalpage\":\""+page.getTotalPages()
				+"\",\"totalsize\":\""+page.getTotalRowsAmount()+"\",\"allprice\":\""+totalprice+"\",\"remoney\":\""+currentuser.getRegMoney()+"\"}");
		response.setCharacterEncoding("UTF-8");
		response.getWriter().print(sb.toString()); 			
	}


}
